package com.xbongbong.saas.script.tempenum.product;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkKeyFieldInfoPojo;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.OriStockEnum;
import com.xbongbong.saas.enums.product.OriStockProductEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/10/12 10:32
 * @since v1.0
 */
@Getter
public enum TempOriStockProductEnum {
    PRODUCT(OriStockProductEnum.PRODUCT),
    UNIT(OriStockProductEnum.UNIT),
    NUM(OriStockProductEnum.NUM),
    BATCH(OriStockProductEnum.BATCH),
    PRODUCE_DATE(OriStockProductEnum.PRODUCE_DATE),
    GUARANTEE_PERIOD(OriStockProductEnum.GUARANTEE_PERIOD),
    EXPIRE_DATE(OriStockProductEnum.EXPIRE_DATE),
    MEMO(OriStockProductEnum.MEMO),
    ;

    private OriStockProductEnum oriStockProductEnum;

    TempOriStockProductEnum(OriStockProductEnum oriStockProductEnum) {
        this.oriStockProductEnum = oriStockProductEnum;
    }

    public static void initProduct(FieldAttrEntity fieldAttrEntity, List<LinkedAttrPoJo> linkedAttrPoJoList,List<LinkedAttrPoJo> linkedShowPoJoList, PaasFormEntityExt paasFormEntity){
        LinkInfoPojo linkInfoPojo = new LinkInfoPojo();
        linkInfoPojo.setLinkBusinessType(XbbRefTypeEnum.PRODUCT.getCode());
        linkInfoPojo.setLinkedAttr(linkedAttrPoJoList);
        linkInfoPojo.setLinkSaasMark(SaasMarkEnum.SAAS.getCode());
        linkInfoPojo.setLinkKey(linkedAttrPoJoList.get(1).getAttr());
        linkInfoPojo.setCondition(new ArrayList<>());
        LinkKeyFieldInfoPojo linkKeyFieldInfoPojo = new LinkKeyFieldInfoPojo();
        BeanUtil.copyProperties(linkedAttrPoJoList.get(1),linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkKeyAttr(linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkedShowAttr(linkedShowPoJoList);
        fieldAttrEntity.setLinkInfo(linkInfoPojo);
        fieldAttrEntity.setShowEditable(1);
    }

    public static void initNum(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultValue(1);
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
    }

    public static void initUnit(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
    }
    public static void initProductDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setDateType("yyyy-MM-dd");
    }

    public static void initGuaranteePeriod(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIntegerOnly(1);
    }



    public static void initExpireDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + OriStockEnum.PRODUCT_DETAIL.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr() + "}+24*60*60*{self." + OriStockEnum.PRODUCT_DETAIL.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + OriStockEnum.PRODUCT_DETAIL.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr(), "关联产品.生产日期");
        labelMap.put("self." + OriStockEnum.PRODUCT_DETAIL.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr(), "关联产品.保质期");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }
}
